<?php

class downloadController extends Controller
{

    public $initphp_list = array(
        'edit',
        'add',
        'save',
        'cat',
        'cat_add',
        'cat_del',
        'cat_save',
        'upload',
        'del'
    );

    /**
     *
     * @var downloadService
     */
    private $downloadService;

    public function __construct()
    {
        parent::__construct();
        $this->checkLogin();
        $this->downloadService = InitPHP::getService("download");
    }

    public function run()
    {
        $data = $this->downloadService->getDownload();
        $this->view->assign("menu_now", "down_list");
        $this->view->assign("data", $data);
        $catArr=$this->downloadService->getCatTree(false);
        $this->view->assign("cat", $catArr);
        $this->view->set_tpl("manage/header", "F"); // 设置公共头部模板
        $this->view->set_tpl("manage/download/down_list");
        $this->view->set_tpl("manage/footer", 'L'); // 设置公共尾部模板
        $this->view->assign("title", "下载管理-凯巴斯-管理后台");
        $this->view->display();
    }

    public function add()
    {
        $this->view->assign("menu_now", "down_list");
        $id = $this->controller->get_gp("id");
        if ($id > 0) {
            $data = $this->downloadService->getOne($id);
            $this->view->assign("id", $id);
            $this->view->assign("data", $data);
        }
        $catArr = $this->downloadService->getCatTree();
        $this->view->assign("cat", $catArr);
        $this->view->set_tpl("manage/header", "F"); // 设置公共头部模板
        $this->view->set_tpl("manage/download/down_add");
        $this->view->set_tpl("manage/footer", 'L'); // 设置公共尾部模板
        $this->view->assign("title", "添加产品-凯巴斯-管理后台");
        $this->view->display();
    }

    public function cat()
    {
        $data = $this->downloadService->getMenu();
        $this->view->assign("menu_now", "download");
        $this->view->assign("data", $data);
        $this->view->set_tpl("manage/header", "F"); // 设置公共头部模板
        $this->view->set_tpl("manage/download/cat_list");
        $this->view->set_tpl("manage/footer", 'L'); // 设置公共尾部模板
        $this->view->assign("title", "产品分类管理-凯巴斯-管理后台");
        $this->view->display();
    }

    public function cat_add()
    {
        $id = $this->controller->get_gp("id");
        if ($id > 0) {
            $data = $this->downloadService->getOneCat($id);
            $pid = $data['pid'];
            $this->view->assign("id", $id);
            $this->view->assign("data", $data);
        } else {
            $pid = $this->controller->get_gp("pid");
        }
        $parent = $this->downloadService->getOneCat($pid);
        $this->view->assign("menu_now", "download");
        $this->view->assign("parent", $parent);
        $this->view->set_tpl("manage/header", "F"); // 设置公共头部模板
        $this->view->set_tpl("manage/download/cat_add");
        $this->view->set_tpl("manage/footer", 'L'); // 设置公共尾部模板
        $this->view->assign("title", "添加分类-凯巴斯-管理后台");
        $this->view->display();
    }

    public function cat_del()
    {
        $id = $this->controller->get_gp("id");
        $this->downloadService->delete_cat($id);
        JsMessage("删除成功！", "/manage/download/cat");
    }

    public function cat_save()
    {
        $id = $this->controller->get_gp("id");
        $data = $this->controller->get_gp(array(
            'pid',
            'title',
            'seo_title',
            'keyword',
            'descript',
            'sort'
        ));
        if (! $data['pid']) {
            $data['pid'] = 0;
        }
        if ($id) {
            $this->downloadService->update_cat($id, $data);
        } else {
            $this->downloadService->add_cat($data);
        }
        JsMessage("保存成功！", '/manage/download/cat');
    }

    public function upload()
    {
        $appid = $this->renderArray['appid'];
        $platform = $this->renderArray['platform'];
        $path = APP_PATH . "../upload/files";
        if (! is_dir($path)) {
            _mkdir($path);
        }
        $new_name = "upload_" . uniqid() . rand(1, 1000);
        $opinion = array(
            'maxSize' => 204800,
            'allowFileType' => array(
                'pdf',
                'doc',
                'zip',
                'rar',
                'jpg',
                'xls',
                'gif'
            )
        );
        $upload = self::getUpload();
        /* 参数：表单名称；上传后新的文件名；存放的文件目录；配置，包含最大文件和文件类型 */
        $result = $upload->upload('upload', $new_name, $path, $opinion);
        if ($result['error'] == 0) {
            echo json_encode(array(
                'status' => 1,
                'size' => $result['size'],
                'data' => "/upload/files/" . $new_name . "." . $result['type']
            ));
            exit();
        } else {
            echo json_encode(array('msg'=>$result['msg']));
        }
    }

    public function edit()
    {
        $id = $this->controller->get_gp("id");
        if ($id > 0) {
            $data = $this->downloadService->getOne($id);
            $pid = $data['pid'];
            $this->view->assign("id", $id);
            $this->view->assign("data", $data);
        } else {
            $pid = $this->controller->get_gp("pid");
        }
        $parent = $this->downloadService->getOne($pid);
        $this->view->assign("menu_now", "download");
        $this->view->assign("parent", $parent);
        $this->view->set_tpl("manage/header", "F"); // 设置公共头部模板
        $this->view->set_tpl("manage/download/add");
        $this->view->set_tpl("manage/footer", 'L'); // 设置公共尾部模板
        $this->view->assign("title", "添加文件-凯巴斯-管理后台");
        $this->view->display();
    }

    public function del()
    {
        $id = $this->controller->get_gp("id");
        $this->downloadService->delete($id);
        JsMessage("删除成功！", "/manage/download/run");
    }

    public function save()
    {
        $id = $this->controller->get_gp("id");
        $data = $this->controller->get_gp(array(
            'pid',
            'title',
            'descript',
            'down_url',
            'descript',
            'file_size'
        ));
        $data['add_time']=time();
        if (! $data['pid']) {
            $data['pid'] = 0;
        }
        if ($id) {
            $this->downloadService->update($id, $data);
        } else {
            $this->downloadService->add($data);
        }
        JsMessage("保存成功！", '/manage/download/run');
    }

    /**
     *
     * @return uploadInit
     */
    private function getUpload()
    {
        return InitPHP::getLibrarys("upload");
    }

    /**
     * 检查登陆状态
     */
    public function checkLogin()
    {
        $session = $this->getUtil('session');
        $adminid = $session->get('adminid');
        if (empty($adminid)) {
            $function = $this->getLibrary('function');
            $function->goto_url('/manage/login');
            die();
        }
        $this->view->assign("adminname", $session->get('adminname'));
        $this->view->assign("adminid", $session->get('adminid'));
    }
}